﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TankeWar.Properties;

namespace TankeWar
{
    internal class GameObjectManager
    {
        //定义墙
        private static List<NotMovething> wallist=new List<NotMovething>();
        //定义钢铁
        private static List<NotMovething> stelllist = new List<NotMovething>();
        //老巢boss
        private static NotMovething boss;
        //定义坦克 我方
        private static MyTanke mytanke;
        //定义敌方坦克
        private static List<EnemyTanke> tankList = new List<EnemyTanke>();
        //定义子弹
        private static List<Bullet> bulletList = new List<Bullet>();
        //对子弹控制的锁
        private static Object _bulletLock = new object();
        //爆炸对象
        private static List<Explosion> expList = new List<Explosion>();
        //敌方产生速度
        private static int enemyBornSpeed = 60;
        //敌方产生数量
        private static int enemyBornCount = 60;

        //坐标点
        private static Point[] points = new Point[3];
        public static void Start()
        {
            points[0].X = 0; points[0].Y = 0;

            points[1].X = 7 * 30; points[1].Y = 0;

            points[2].X = 14 * 30; points[2].Y = 0;

        }
        public static void Update()
        {
            List<Bullet> templist = new List<Bullet>();
            templist.Clear();
            templist.AddRange(bulletList);
            foreach (NotMovething nm in wallist)
            {
                nm.Update();
            }
            foreach (NotMovething nm in stelllist)
            {
                nm.Update();
            }

            foreach (EnemyTanke tank in tankList)
            {
                tank.Update();
            }
            //检测和销毁子弹
            CheckAndDestroyBullet();
            foreach (Bullet bullet in templist)
            {
                bullet.Update();
            }

            foreach (Explosion exp in expList)
            {
                exp.Update();
            }
            //检测和销毁爆炸动画
            CheckAndDestroyExplosion();

            boss.Update();

            mytanke.Update();

            EnemyBorn();
        }
        private static void CheckAndDestroyBullet()
        {
            List<Bullet> needToDestroy = new List<Bullet>();
            foreach (Bullet bullet in bulletList)
            {
                if (bullet.IsDestroy == true)
                {
                    needToDestroy.Add(bullet);
                }
            }
            foreach (Bullet bullet in needToDestroy)
            {
                bulletList.Remove(bullet);
            }
        }
        private static void CheckAndDestroyExplosion()
        {
            List<Explosion> needToDestroy = new List<Explosion>();
            foreach (Explosion exp in expList)
            {
                if (exp.IsNeedDestroy == true)
                {
                    needToDestroy.Add(exp);
                }
            }
            foreach (Explosion exp in needToDestroy)
            {
                expList.Remove(exp);
            }
        }
        //产生爆炸
        public static void CreateExplosion(int x, int y)
        {
            Explosion exp = new Explosion(x, y);
            expList.Add(exp);
        }
        //产生子弹
        public static void CreateBullet(int x, int y, Tag tag, Direction dir)
        {
            Bullet bullet = new Bullet(x, y, 8, dir, tag);

            bulletList.Add(bullet);

        }
        //销毁墙
        public static void DestroyWall(NotMovething wall)
        {
            wallist.Remove(wall);
        }
        //销毁敌方坦克
        public static void DestroyTank(EnemyTanke tank)
        {
            tankList.Remove(tank);
        }
        //产生敌方坦克
        private static void EnemyBorn()
        {
            enemyBornCount++;
            if (enemyBornCount < enemyBornSpeed) return;

            SoundManager.PlayAdd();
            //0-2
            Random rd = new Random();
            int index = rd.Next(0, 3);
            Point position = points[index];
            int enemyType = rd.Next(1, 5);
            switch (enemyType)
            {
                case 1:
                    CreateEnemyTank1(position.X, position.Y);
                    break;
                case 2:
                    CreateEnemyTank2(position.X, position.Y);
                    break;
                case 3:
                    CreateEnemyTank3(position.X, position.Y);
                    break;
                case 4:
                    CreateEnemyTank4(position.X, position.Y);
                    break;

            }

            enemyBornCount = 0;
        }
        private static void CreateEnemyTank1(int x, int y)
        {
            EnemyTanke tank = new EnemyTanke(x, y, 2, Resources.GrayDown, Resources.GrayUp, Resources.GrayRight, Resources.GrayLeft);
            tankList.Add(tank);
        }
        private static void CreateEnemyTank2(int x, int y)
        {

            EnemyTanke tank = new EnemyTanke(x, y, 2, Resources.GreenDown, Resources.GreenUp, Resources.GreenRight, Resources.GreenLeft);
            tankList.Add(tank);
        }
        private static void CreateEnemyTank3(int x, int y)
        {

            EnemyTanke tank = new EnemyTanke(x, y, 4, Resources.QuickDown, Resources.QuickUp, Resources.QuickRight, Resources.QuickLeft);
            tankList.Add(tank);
        }
        private static void CreateEnemyTank4(int x, int y)
        {

            EnemyTanke tank = new EnemyTanke(x, y, 1, Resources.SlowDown, Resources.SlowUp, Resources.SlowRight, Resources.SlowLeft);
            tankList.Add(tank);
        }
        public static NotMovething IsCollidedWall(Rectangle rt)
        {
            foreach (NotMovething wall in wallist)
            {
                if (wall.GetRectangle().IntersectsWith(rt))
                {
                    return wall;
                }
            }
            return null;
        }
        public static NotMovething IsCollidedSteel(Rectangle rt)
        {
            foreach (NotMovething wall in stelllist)
            {
                if (wall.GetRectangle().IntersectsWith(rt))
                {
                    return wall;
                }
            }
            return null;
        }
        //是否撞到boss
        public static bool IsCollidedBoss(Rectangle rt)
        {
            return boss.GetRectangle().IntersectsWith(rt);
        }
        //是否撞到我方坦克
        public static MyTanke IsCollidedMyTank(Rectangle rt)
        {
            if (mytanke.GetRectangle().IntersectsWith(rt)) return mytanke;
            else return null;
        }
        //是否撞到敌方坦克
        public static EnemyTanke IsCollidedEnemyTank(Rectangle rt)
        {
            foreach (EnemyTanke tank in tankList)
            {
                if (tank.GetRectangle().IntersectsWith(rt))
                {
                    return tank;
                }
            }
            return null;
        }
        //创建我方坦克
        public static void CreateMyTank()
        {
            int x = 5 * 30;
            int y = 14 * 30;

            mytanke = new MyTanke(x, y, 2);
        }
        public static void CreateMap()
        {
            CreateWall(1, 1, 5, Resources.wall, wallist);
            CreateWall(3, 1, 5, Resources.wall, wallist);
            CreateWall(5, 1, 4, Resources.wall, wallist);
            CreateWall(7, 1, 3, Resources.wall, wallist);
            CreateWall(9, 1, 4, Resources.wall, wallist);
            CreateWall(11, 1, 5, Resources.wall, wallist);
            CreateWall(13, 1, 5, Resources.wall, wallist);

            CreateWall(7, 5, 1, Resources.steel, stelllist);

            CreateWall(0, 7, 1, Resources.steel, stelllist);

            CreateWall(2, 7, 1, Resources.wall, wallist);
            CreateWall(3, 7, 1, Resources.wall, wallist);
            CreateWall(4, 7, 1, Resources.wall, wallist);
            CreateWall(6, 7, 1, Resources.wall, wallist);
            CreateWall(7, 6, 2, Resources.wall, wallist);
            CreateWall(8, 7, 1, Resources.wall, wallist);
            CreateWall(10, 7, 1, Resources.wall, wallist);
            CreateWall(11, 7, 1, Resources.wall, wallist);
            CreateWall(12, 7, 1, Resources.wall, wallist);

            CreateWall(14, 7, 1, Resources.steel, stelllist);

            CreateWall(1, 9, 5, Resources.wall, wallist);
            CreateWall(3, 9, 5, Resources.wall, wallist);
            CreateWall(5, 9, 3, Resources.wall, wallist);

            CreateWall(6, 10, 1, Resources.wall, wallist);
            CreateWall(7, 10, 2, Resources.wall, wallist);
            CreateWall(8, 10, 1, Resources.wall, wallist);

            CreateWall(9, 9, 3, Resources.wall, wallist);
            CreateWall(11, 9, 5, Resources.wall, wallist);
            CreateWall(13, 9, 5, Resources.wall, wallist);


            CreateWall(6, 13, 2, Resources.wall, wallist);
            CreateWall(7, 13, 1, Resources.wall, wallist);
            CreateWall(8, 13, 2, Resources.wall, wallist);

            CreateBoss(7, 14, Resources.Boss);
        }
        private static void CreateBoss(int x, int y, Image img)
        {
            int xPosition = x * 30;
            int yPosition = y * 30;
            boss = new NotMovething(xPosition, yPosition, img);
        }

        private static void CreateWall(int x, int y, int count, Image img, List<NotMovething> wallList)
        {
            int xPosition = x * 30;
            int yPosition = y * 30;
            for (int i = yPosition; i < yPosition + count * 30; i += 15)
            {
                // i xPosition     i xPosition+15
                NotMovething wall1 = new NotMovething(xPosition, i, img);
                NotMovething wall2 = new NotMovething(xPosition + 15, i, img);
                wallList.Add(wall1);
                wallList.Add(wall2);
            }
        }
        public static void KeyDown(KeyEventArgs args)
        {
            mytanke.KeyDown(args);
        }
        public static void KeyUp(KeyEventArgs args)
        {
            mytanke.KeyUp(args);
        }

    }
}
